<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: ProductController.php 465 2012-02-12 05:33:23Z beimuaihui $
 */



class Mall_ProductController extends Baogg_Controller_Action {

	function init() {
		Zend_Loader::loadClass ( 'Product' );
		$this->product = new Product ( );

	}

	/**
	 * The default action
	 */
	public function listAction() {
		global $LANG;
		
		
		Zend_Loader::loadClass("Baogg_View_Extgrid");
		
		$cm=array();
		$cm['product_id']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_PK) ;
		$cm['product_name']=array("sortable"=>true,"width"=>420);
		$cm['main_table']=array("sortable"=>true);
		$cm['ext_table']=array("sortable"=>true);		
		$cm['state']=array("sortable"=>true);
		$cm['ordering']=array("sortable"=>true);
		//$cm['sub_table']=array("hidden"=>true,"cmType"=>Baogg_View_Extgrid::CMTYPE_SUBTABLE) ; //Baogg_View_Extgrid::;
		
		
		$grid=new Baogg_View_Extgrid(BAOGG_BASE_URL."system/product/data", $cm);
		$grid->setSelectionModel(true);
		$grid->setRowNumberer(true);
	//$grid->registerButton("delete","grid.deleteSelected('".BAOGG_BASE_URL."system/product/delete/')");
	//$grid->registerButton("fields","window.location.href='".BAOGG_BASE_URL."system/productfield/list/';");
		
		$this->view->form_url=BAOGG_BASE_URL."system/product/save";
		$this->view->LANG=Zend_Json::encode($LANG);
		$this->view->grid = $grid->render();
		
		//echo '<pre>';var_dump($LANG);exit;


	}

	/**
	 * The data action
	 */
	function dataAction() {
	    $post=$this->_request->getPost ();
	    $where=array();$order=array();$limit=array();
	   foreach((array)$post as $k=>$v){
	       if($k=="start" || $k=="limit"){
	           $limit[$k]=$v;
	       }else if($k=="sort" || $k=="dir"){
	           $order[$k]=$v;
	       }else{
	           if($k=="product_id"  ){
	               if($v && is_numeric($v)){
	                   $v=(int)$v;
	                   $where[$k]=$v;
	               }
	           }else{
	               $v='%'.$v.'%';
	               $where[$k]=$v;
	           }
	           
	       }
	   }

		$this->view->grid = Zend_Json::encode(array("count"=>"1","data"=>$this->product->getList($where,$order,$limit)));
	}

	
	/**
	 * The add action
	 */
	function addAction(){
		if ($this->_request->isPost ()) {
			$this->saveAction();
		}else{
			$this->editAction();
			$this->_helper->viewRenderer->render ( 'edit' );
		}
	}
	
	/**
	 * The edit action
	 */
	function editAction() {
		

	/*	$id = (int)Baogg_Controller_Action::_getParam ( 'id' );
		if ($id>0) {
			$rows = $this->product->getOne ( $id );
			$list=$rows[0];
			$action="edit";
			$urlParam="/id/".$id;
		} else {
			$action="add";
			$urlParam="";
			$list["state"]=1;
		}

		//toolbar
		Loader::loadClass('ext::toolbar');
		$this->view->toolbarButton = new extToolBar();
		$this->view->toolbarButton->registerButton ($multiLang->g_save, "handleForm('adminForm',\"{$config['web']['baseurl']}multilang/product/save{$urlParam}\")", 'save-icon' );
		$this->view->toolbarButton->registerButton ( $multiLang->g_cancel, 'parent.dialog.hide()', 'cancel-icon' );
		//form
		Loader::loadClass('form');
		$handleform = new form_element();
		$form = $this->product->getForm ();
		if(!$list){
			//$_list=array_fill_keys(array_keys($form['elements']),"");
			//$list[0]=$_list;
		}
		//$list[0]['action']
		$form = $handleform->handleArray($form['options'],$form['elements'], $list,$action);
		$this->view->form = $form;*/
	}

	/**
	 * The delete action
	 */
	function deleteAction() {
        
	    $ret=array("success"=>false,"msg"=>"delete_fail");
	    $id = Baogg_Controller_Action::_getParam ( 'ids' );
		 
		   
		
		if ($this->product->_delete ( $id )){
			$ret['success'] = true;
			$ret["msg"]="delete_ok";
		}
		
		echo Baogg_Language::outputResult($ret);
		   exit;
		/*$this->_helper->viewRenderer->setNoRender ();
		$log=array();
		$log['module_permission_id'] 	= getPermissionId();
		$log['products_id']				= $_SESSION['s_admin_product_id'];
		$log['message']					= $message;
		$log['log_sql']					= debugDb();
		$log['result']					= $flag;
		$log['log_products_id']			= $_SESSION['s_admin_product_id'];
		//print_r($log);
		insertLog($log);*/
		
		
	}

	/**
	 * The save action
	 */
	function saveAction() {
		//global $LANG;
		
	    $ret=array("success"=>false,"msg"=>"save_fail");
	    
	    //check form
		if (!$this->_request->isPost ())
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;
		}
		
		$formData = $this->_request->getPost ();

		if(!$formData)
		{
		   echo Baogg_Language::outputResult($ret);
		   exit;     
		}
			
		$arr=$this->product->filterForm($formData);
        
		$rs = $this->product->getSame($arr);
		if(($arr["product_id"])>0){
			//update record
			//$permission_type = 2;
			//echo '<pre>';print_r($rs);var_dump(count($rs));exit;
			if( count($rs)==1 &&  $rs[0] == $arr["product_id"] ){
			    $this->product->_update($arr,$arr["product_id"]);
			    $ret['success'] = true;				
				$ret['msg'] = "save_ok";			
			}else{
				$ret['msg'] = "exist_same_record";
			}
			
		}else{
			//insert record
			//$permission_type = 1;			
			if($rs){				
				$ret['msg'] = "exist_same_record";
			}else{
			    unset($arr["product_id"]);
				$ret['success'] = $this->product->_insert($arr)>0?true:false;
				$ret['msg'] = "add_ok";
			}
		}
				
		echo Baogg_Language::outputResult($ret);
		
		$this->_helper->viewRenderer->setNoRender ();
		
		
	}

}
